/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/11/13
 * Time: 2:23 PM
 * To change this template use File | Settings | File Templates.
 */
var MyExporter = Class.create({
    initialize: function(){

    },
    export: function(book, doneCallback){
        var data = new Object();
        data['name'] = book.name;
        data['id'] = book.id;
        data['resourcesPath'] = book.id + '/resources/';
        data['coverImage'] = book.coverImage.replace(Static.uploaderPath, "");
        data['firstPageId'] = book.pages[0].id;
        data['pages'] = new Array();
        for(var i = 0; i < book.pages.length; i++){
            var p = book.pages[i];

            var pageJson = new Object();
            pageJson['id'] = p.id;
            pageJson['elements'] = new Array();

            for(var j = 0; j < p.elements.length;j++){
                var e = new Object();
                e['element'] = p.elements[j].attributeList.toDataObjectArray(true);

                pageJson['elements'].push(e);
            }

            data['pages'].push(pageJson);
        }

        $j.post('exportBook.php',
            {
                data: $j.toJSON( data ),
                fileName: Global.currentBook.id,
                bookName: Global.currentBook.name,
                coverImage: Global.currentBook.coverImage.replace(Static.uploaderPath, "")
            }
        ).done(function(data){
                // call the callback function
                doneCallback();
            });
    }
});